package org.webscale.http;

import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.StringReader;
import java.util.Iterator;

import junit.framework.TestCase;

import org.apache.log4j.Logger;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.http.ConnectionManager;
import org.htmlparser.lexer.Page;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;

public class TestHTMLParser extends TestCase {

	static String file = "org/webscale/http/streetdetails.html";
	static String url1 = "file:///C:/HomeValues/StreetDetails/93065_WISHARD.html";
	static String url2 = "file:///C:/HomeValues/StreetDetails/93065_WELLER.html" ; //CASE with not even odd
	static File dir = new File("C:\\HomeValues\\StreetDetails");
	
	Parser parser = new Parser ();
	
	public void testParser() throws Exception{
		
		String [] files = dir.list(new FilenameFilter() {
		
			public boolean accept(File dir, String name) {
				if(name.endsWith(".html"))
					return true;
				else
					return false;
			}
		
		});
		for(int i=0 ; i < files.length ;i++){
			File f = new File(dir ,files[i]);
			String url = f.toURI().toString();
			System.out.println("****FILE****** " + url);
			try{
			parseOneLine(url ,"table");
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void parseOneLine(String url , String tag) throws Exception {
      //InputStream is = this.getClass().getClassLoader().getResourceAsStream(file);
		String [] args = new String[2];
		
		args[0] = url;
		args[1] = tag;
		//org.htmlparser.Parser.main(args);
		TagNameFilter filter;
		
        filter = new TagNameFilter (args[1]);
        getConnectionManager ().setRedirectionProcessingEnabled (false);
        getConnectionManager ().setCookieProcessingEnabled (true);
        parser.setResource (args[0]);
        NodeList list = parser.parse (filter);
        
        Node[] nodes = list.toNodeArray();
        int i = nodes.length;
        
        Node n = nodes[i-1];
        String str = n.toHtml();
        System.out.println (str);
        list = processNodeForTag(n , "tr");
        processStreetDetailsTable(list);
        /*
        nodes = list.toNodeArray();
        for(int y =2 ; y < nodes.length;y++){
        	processNodeForTag(nodes[y] , "td");
        }
        */
      
        
	}
	
	
	
	private void processStreetDetailsTable(NodeList nl)throws Exception{
		SimpleNodeIterator it = nl.elements();
		Node header = it.nextNode();
		System.out.println("*** NODE-HEADER **** " + header.toHtml());
		
		Node title = it.nextNode();
		System.out.println("*** NODE-HEADER **** " + title.toHtml());
		
		while(it.hasMoreNodes()){
			Node node = it.nextNode();
			
			int index = nl.indexOf(node);
			System.out.println("*** NODE **** " + index );
			if((index +1) < nl.size()){
				StreetInfo info = new StreetInfo(node);
			}
			
		}
		
	}
	
	 private  NodeList processNodeForTag(Node n , String tag) throws Exception{
		 //Parser p = new Parser();
		 TagNameFilter filter = new TagNameFilter (tag);
		 parser.setInputHTML(n.toHtml());
		 NodeList nl = parser.parse(filter);
		
		 return nl;
	 }
	
	public static ConnectionManager getConnectionManager ()
    {
        return (Page.getConnectionManager ());
    }
	
	public class StreetInfo {
	  	int start;
	  	int end;
	  	boolean even = false;
	  	boolean odd = false;
	  	String street;
	  	String zip4;
	  	String typeOfProperty;
	  	
	  	Node node;
	  	NodeList nlist;
	  	
	  	public StreetInfo(Node n) throws Exception{
	  		super();
	  		node = n;
	  		nlist = processNodeForTag(n , "td");
	  		process();
	  		/*
	  		SimpleNodeIterator it = nlist.elements();
	  		while(it.hasMoreNodes()){
				Node node = it.nextNode();
				System.out.println("\t *** ELE **** " + node.toHtml());
			}
			*/
	  	}
	  	
	  	public void process() throws Exception{
	  		SimpleNodeIterator it = nlist.elements();
	  		processRange(it.nextNode());
	  		processPre(it.nextNode());
	  		processStreet(it.nextNode());
	  		processZip(it.nextNode());
	  		processType(it.nextNode());
	  		
	  	}

	  	private void processRange(Node node) {
	  		String line =node.toPlainTextString();
	  		int eindex = line.indexOf("Even");
	  		int oindex = line.indexOf("Odd");
	  		CharSequence range = line;
	  		String[] splits = line.split("to");
	  		if(eindex > 0){
	  			even = true;
	  			range = line.subSequence(0, eindex);
	  			StringBuffer buf = new StringBuffer(range);
	  			splits = buf.toString().split("to");
	  			
	  			
	  		}
	  		if(oindex > 0){
	  			odd = true;
	  			range = line.subSequence(0, oindex);
	  			StringBuffer buf = new StringBuffer(range);
	  			splits = buf.toString().split("to");
	  		}
	  		
	  		start = Integer.parseInt(splits[0].trim());
	  		end = Integer.parseInt(splits[1].trim());
	  		System.out.println("\t *** ELE -start " + start+ " end" + end + " even= " +even + " odd=" + odd );
	  		
			
		}
	  	
		private void processType(Node nextNode) {
			System.out.println("\t *** ELE **** " + nextNode.toPlainTextString());
			
		}

		private void processZip(Node nextNode) {
			System.out.println("\t *** ELE **** " + nextNode.toPlainTextString());
			
		}

		private void processStreet(Node nextNode) {
			System.out.println("\t *** ELE **** " + nextNode.toPlainTextString());
			
		}

		private void processPre(Node nextNode) {
			System.out.println("\t *** ELE **** " + nextNode);
			
		}

		
	  	
	  	
	  	
	}
}
